package bmt.audioplayer.app.util;

import android.media.MediaPlayer;

/**
 * Created by Tuan on 4/20/14.
 */
public class PlayerHelper {

    /***
     * Convert mili seconds to string timer
     * @param miliSeconds
     * @return
     */
    public static String miliSecondsToTimer(long miliSeconds) {
        String finalTimer = "";
        String finalHour = "";
        String finalMinutes = "";
        String finalSecond = "";

        int hours = (int) (miliSeconds / (60 * 60 * 1000));
        int minutes = (int) (miliSeconds % (60 * 60 * 1000)) / (1000 * 60);
        int seconds = (int) ((miliSeconds % (60 * 60 * 1000)) % (1000 * 60) / 1000);

        if (hours < 10 && hours != 0) {
            finalHour = "0" + hours + ":";
        } else if (hours >= 10) {
            finalHour = hours + ":";
        }

        if (minutes < 10) {
            finalMinutes = "0" + minutes;
        }else{
            finalMinutes = minutes + "";
        }

        if (seconds < 10){
            finalSecond = "0" + seconds;
        }else{
            finalSecond = seconds + "";
        }

        finalTimer = finalHour + finalMinutes + ":" + finalSecond;

        return finalTimer;
    }

    /***
     * Get progress percentage
     * @param currentDuration
     * @param totalDuration
     * @return
     */
    public int getProgressPercentage(long currentDuration, long totalDuration){
        Double percentage = (double) 0;
        long currentSeconds = (int) (currentDuration / 1000);
        long totalSeconds = (int) (totalDuration / 1000);

        percentage = (((double)currentSeconds) / totalSeconds) * 100;

        return percentage.intValue();
    }

    /***
     * Function to change progress to timer
     * @param progress
     * @param totalDuration
     * @return
     */
    public int progressToTimer(int progress, long totalDuration){
        int currentDuration = 0;
        totalDuration = (int) (totalDuration / 1000);
        currentDuration = (int)((((double)progress) / 100) * totalDuration);

        return currentDuration * 1000;
    }

    public long getTimeLeft(MediaPlayer mediaPlayer) {
        long duration = mediaPlayer.getDuration();
        long currentPosition = mediaPlayer.getCurrentPosition();
        long timeLeft = duration - currentPosition;
        return timeLeft;
    }

}
